package com.gxuwz.bignews.model.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.gxuwz.bignews.model.entity.NewsFavorite;
import com.gxuwz.bignews.db.DatabaseHelper;
import java.util.ArrayList;
import java.util.List;

/**
 * 新闻关注DAO，负责关注表的增删查。
 */
public class NewsFavoriteDao {
    private final DatabaseHelper dbHelper;

    public NewsFavoriteDao(Context context) {
        dbHelper = new DatabaseHelper(context);
    }

    /**
     * 插入关注（关注新闻）
     */
    public void insertFavorite(NewsFavorite favorite) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DatabaseHelper.COLUMN_FAVORITE_PHONE, favorite.getPhone());
        values.put(DatabaseHelper.COLUMN_FAVORITE_UNIQUEKEY, favorite.getUniquekey());
        values.put(DatabaseHelper.COLUMN_FAVORITE_TITLE, favorite.getTitle());
        values.put(DatabaseHelper.COLUMN_FAVORITE_THUMBNAIL, favorite.getThumbnailPic());
        values.put(DatabaseHelper.COLUMN_FAVORITE_URL, favorite.getUrl());
        values.put(DatabaseHelper.COLUMN_FAVORITE_TIME, favorite.getTime());
        db.insert(DatabaseHelper.TABLE_FAVORITE, null, values);
        db.close();
    }

    /**
     * 删除关注（取消关注）
     */
    public void deleteFavorite(String phone, String uniquekey) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(DatabaseHelper.TABLE_FAVORITE,
                DatabaseHelper.COLUMN_FAVORITE_PHONE + "=? AND " + DatabaseHelper.COLUMN_FAVORITE_UNIQUEKEY + "=?",
                new String[]{phone, uniquekey});
        db.close();
    }

    /**
     * 查询某用户所有关注新闻，按时间倒序排列
     */
    public List<NewsFavorite> getAllFavoritesByPhone(String phone) {
        List<NewsFavorite> list = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DatabaseHelper.TABLE_FAVORITE, null,
                DatabaseHelper.COLUMN_FAVORITE_PHONE + "=?",
                new String[]{phone}, null, null,
                DatabaseHelper.COLUMN_FAVORITE_TIME + " DESC");
        while (cursor.moveToNext()) {
            NewsFavorite favorite = new NewsFavorite();
            favorite.setId(cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_FAVORITE_ID)));
            favorite.setPhone(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_FAVORITE_PHONE)));
            favorite.setUniquekey(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_FAVORITE_UNIQUEKEY)));
            favorite.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_FAVORITE_TITLE)));
            favorite.setThumbnailPic(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_FAVORITE_THUMBNAIL)));
            favorite.setUrl(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_FAVORITE_URL)));
            favorite.setTime(cursor.getLong(cursor.getColumnIndexOrThrow(DatabaseHelper.COLUMN_FAVORITE_TIME)));
            list.add(favorite);
        }
        cursor.close();
        db.close();
        return list;
    }

    /**
     * 判断某新闻是否已被关注
     */
    public boolean isFavorite(String phone, String uniquekey) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DatabaseHelper.TABLE_FAVORITE, null,
                DatabaseHelper.COLUMN_FAVORITE_PHONE + "=? AND " + DatabaseHelper.COLUMN_FAVORITE_UNIQUEKEY + "=?",
                new String[]{phone, uniquekey}, null, null, null);
        boolean exists = cursor.moveToFirst();
        cursor.close();
        db.close();
        return exists;
    }
} 